package com.tebiecloud.mpweixin.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tebiecloud.mpweixin.client.model.entity.MpminiAccountRef;
import com.tebiecloud.mpweixin.server.service.MpminiAccountRefService;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Map;

/**
 * 小程序登录账号关联登录的账号id 前端控制器
 * @author tebie
 * @date 2019-09-27
 */
@RestController
@RequestMapping("/mpminiaccountref")
public class MpminiAccountRefController {

    @Autowired
    private MpminiAccountRefService mpminiAccountRefService;

    /**
     * 获取分页数据
     * @param map
     * @return
     */
    @GetMapping(value = "list")
    public ResultBody<IPage<MpminiAccountRef>> list(@RequestParam(required = false) Map map) {
        PageParams pageParams = new PageParams(map);
        MpminiAccountRef query = pageParams.mapToObject(MpminiAccountRef.class);
        QueryWrapper<MpminiAccountRef> queryWrapper = new QueryWrapper();
        return ResultBodyUtils.ok(mpminiAccountRefService.page(new PageParams(map), queryWrapper));
    }

    /**
     * 根据ID查找数据
     * @param id
     * @return
     */
    @GetMapping("get")
    public ResultBody<MpminiAccountRef> get(@RequestParam("id") Long id) {
        MpminiAccountRef entity = mpminiAccountRefService.getById(id);
        return ResultBodyUtils.ok(entity);
    }

    /**
     * 添加数据
     * @param accountId 登录的账号id,
     * @param accountUsernameRef 关联的另一登录账号
     * @return
     */
    @PostMapping("add")
    public ResultBody add(
        @RequestParam(value = "accountId", required = false) Long accountId,
        @RequestParam(value = "accountUsernameRef", required = false) String accountUsernameRef
    ) {
        MpminiAccountRef entity = new MpminiAccountRef();
        entity.setAccountId(accountId);
        entity.setAccountUsernameRef(accountUsernameRef);
        mpminiAccountRefService.save(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 更新数据
     * @param accountId 登录的账号id
     * @param accountUsernameRef 关联的另一登录账号
     * @return
     */
    @PostMapping("update")
    public ResultBody update(
            @RequestParam(value = "accountId", required = false) Long accountId,
            @RequestParam(value = "accountUsernameRef", required = false) String accountUsernameRef
    ) {
        MpminiAccountRef entity = new MpminiAccountRef();
        entity.setAccountId(accountId);
        entity.setAccountUsernameRef(accountUsernameRef);
        mpminiAccountRefService.updateById(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 删除数据
     * @param id 单个id
     * @param ids 多个id用,号隔开
     * @return
     */
    @PostMapping("remove")
    public ResultBody remove(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "ids", required = false) String ids
    ) {
        if(id != null && id > 0) {
            mpminiAccountRefService.removeById(id);
        }
        if(ids != null && ids.length() > 0) {
            mpminiAccountRefService.removeByIds(Arrays.asList(ids.split(",")));
        }
        return ResultBodyUtils.ok();
    }

}
